﻿@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="Env-AddMonitoring-MonitoringId">@ViewData["MonitoringId"]</label>
            <label id="Env-AddMonitoring-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">标准类型*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="Env-AddMonitoring-Select-Type" class="form-control font-rfs border-radius-noleft" style="width: 90%" onchange="selectOnchang(this)"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left">
                <input id="Env-AddMonitoring-Input-Type" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" />
            </div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">标准名称*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="Env-AddMonitoring-Name" class="form-control font-rfs border-radius-noleft" style="width:90%" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">温度高限</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="Env-AddMonitoring-HighTemperature" class="form-control font-rfs border-radius-noleft" style="width:45%" placeholder="有效数字" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">温度低限</span>
            <input id="Env-AddMonitoring-LowTemperature" class="form-control font-rfs border-radius-noleft" style="width:32%" placeholder="有效数字" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">湿度高限</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="Env-AddMonitoring-HighHumidity" class="form-control font-rfs border-radius-noleft" style="width:45%" placeholder="有效数字" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">湿度低限</span>
            <input id="Env-AddMonitoring-LowHumidity" class="form-control font-rfs border-radius-noleft" style="width:32%" placeholder="有效数字" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">标准描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Env-AddMonitoring-Description" class="form-control font-rfs border-radius-noleft" />
        </div>
    </div>
    <div class="text-right">
        <button id="Env-AddMonitoring-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Env-AddMonitoring-cancel" type="button" class="btn btn-info font-rfs" data-dismiss="modal">取消</button>
    </div>
</div>

<script>

    $(document).ready(function () {
        LoadType();
        if ($("#Env-AddMonitoring-MonitoringId").html().length > 0) {
            LoadMonitoringById();
        }
    });

    function LoadType() {
        $.post("GetMonitoringType", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#Env-AddMonitoring-Select-Type").append(new Option(data[i].type));
            }
            $("#Env-AddMonitoring-Select-Type").prepend(new Option("", ""));
            $("#Env-AddMonitoring-Select-Type").val("");
        });
    }

    function LoadMonitoringById() {
        $.post("GetMonitoringById", {
            Id: $("#Env-AddMonitoring-MonitoringId").html()
        }, function (data) {
            $("#Env-AddMonitoring-Name").val(data.name);
            $("#Env-AddMonitoring-Input-Type").val(data.type);
            $("#Env-AddMonitoring-HighTemperature").val(data.highTemperature);
            $("#Env-AddMonitoring-LowTemperature").val(data.lowTemperature);
            $("#Env-AddMonitoring-HighHumidity").val(data.highHumidity);
            $("#Env-AddMonitoring-LowHumidity").val(data.lowHumidity);
            $("#Env-AddMonitoring-Description").val(data.description);
        });
    }

    function selectOnchang(obj) {
        $("#Env-AddMonitoring-Input-Type").val($("#Env-AddMonitoring-Select-Type").find("option:selected").text());
    };

    $("#Env-AddMonitoring-confirm").click(function () {
        if ($.trim($("#Env-AddMonitoring-Input-Type").val()).length == 0) {
            alert("标准类型不能为空！");
            $("#Env-AddMonitoring-Input-Type").addClass('error-style');
            return false;
        }
        if ($.trim($("#Env-AddMonitoring-Name").val()).length == 0) {
            alert("标准名称不能为空！");
            $("#Env-AddMonitoring-Name").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Name"] = $.trim($("#Env-AddMonitoring-Name").val());
        jsonStr["Type"] = $.trim($("#Env-AddMonitoring-Input-Type").val());
        jsonStr["HighTemperature"] = $.trim($("#Env-AddMonitoring-HighTemperature").val());
        jsonStr["LowTemperature"] = $.trim($("#Env-AddMonitoring-LowTemperature").val());
        jsonStr["HighHumidity"] = $.trim($("#Env-AddMonitoring-HighHumidity").val());
        jsonStr["LowHumidity"] = $.trim($("#Env-AddMonitoring-LowHumidity").val());
        jsonStr["Description"] = $.trim($("#Env-AddMonitoring-Description").val());

        var AddMonitoringId = $("#Env-AddMonitoring-MonitoringId").html();
        if (AddMonitoringId == "") {
            $.post("postAddMonitoring", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                alert(data);
            });
        } else {
            jsonStr["Id"] = AddMonitoringId;
            $.post("UpdateMonitoring", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    var modalID = $("#Env-AddMonitoring-Parent-ModalID").html();
                    $("#" + modalID).modal('hide');
                } else {
                    alert(data);
                }
            });
        }
    });

    $("#Env-AddMonitoring-cancel").click(function () {
        var modalID = $("#Env-AddMonitoring-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

</script>
